package gui;

import biz.*;
import javafx.application.Application;
import javafx.stage.Stage;

import javafx.scene.layout.VBox;
import javafx.scene.Scene;
import javafx.scene.image.Image;

public class MainApp extends Application{

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        final int mapWidth = 30, mapHeight = 20, viewWidth = 20, viewHeight = 12;


        // 业务类
        GameMap map = new GameMap(mapWidth, mapHeight);
        for(int x = 0; x < mapWidth; x++) {
            for(int y = 0; y < mapHeight; y++) {
                map.put(x, y, (int)(Math.random() * 3));
            }
        }
        ViewArea view = new ViewArea(0, 0, viewWidth, viewHeight);
        view.setTarget(map);
        Controller c = new Controller(view);
        c.addBall(new Ball(10, 10));
        c.addBall(new Ball(12, 3));
        c.addBall(new Ball(4, 6));

        // 业务相关界面类
        MapViewer viewer = new MapViewer(c, 64);
        String pic_dir = "file:res\\";
        viewer.put("map0", new Image(pic_dir + "001.png"));
        viewer.put("map1", new Image(pic_dir +"002.png"));
        viewer.put("map2", new Image(pic_dir +"003.png"));
        viewer.put("ball0", new Image(pic_dir +"football01.png"));
        viewer.put("ball1", new Image(pic_dir +"football02.png"));


        // 游戏循环，动态要素
        GameLoop gl = new GameLoop() {

            @Override
            public void preprocess() {}

            @Override
            public void refresh() {
                c.executeAll();
            }

            @Override
            public void display() {
                viewer.render();
            }

        };

        gl.start();

        // 界面布局
        VBox box = new VBox();
        box.getChildren().addAll(viewer);

        Scene scene = new Scene(box);

        primaryStage.setScene(scene);
        primaryStage.setResizable(false);
        primaryStage.setTitle("小游戏");
        primaryStage.show();
    }

}
